package cc.fs.myproject;

import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

/**
 * Created by fostion on 2015/6/17.
 */
public class StartActivity extends BaseActivity {

    private ImageView img;
    private int screenWidth;
    private int screenHeight;
    private float scale = 1.0f;
    private float scaleHeight;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_start);

        img = (ImageView)findViewById(R.id.img);

        setUpAnimation();
        runDelayed(new Runnable() {
            @Override
            public void run() {
                MainActivity.start(_this);
                finish();
            }
        }, 3000);
    }

    public void setUpAnimation(){
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int statusBarHeight = (int) Math.ceil( 25 * metrics.density);
        Display display = getWindowManager().getDefaultDisplay();
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
        Bitmap srcBitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();

        int picWidth = srcBitmap.getWidth();
        int picHeight = srcBitmap.getHeight();
        scale = 1.0f*screenWidth/picWidth;
        scaleHeight = picHeight * scale;

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f, scaleHeight - screenHeight + statusBarHeight);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float y = -(Float) animation.getAnimatedValue();
                Matrix mMatrix = new Matrix();
                mMatrix.postScale(scale,scale);
                mMatrix.postTranslate(0,y);
                img.setImageMatrix(mMatrix);
            }
        });
        valueAnimator.setDuration(2000);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }

}
